package com.ruoyi.psi.inventory.in.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.inventory.in.domain.InventoryIn;
import com.ruoyi.psi.inventory.in.domain.InventoryInList;
import com.ruoyi.psi.inventory.in.mapper.InventoryInMapper;
import com.ruoyi.utils.ID.IdUtil;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 其他入库
 * 业务层执行
 */
@Service
public class InventoryInServiceImpl implements IInventoryInService {
    @Autowired
    private InventoryInMapper inventoryInMapper;

    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;


    /**
     * 其他入库查询
     *
     * @param inventoryIn
     * @return
     */
    @Override
    public List<InventoryIn> selectInventoryInList(InventoryIn inventoryIn) {
        List<InventoryIn> inventoryIns = inventoryInMapper.selectInventoryInList(inventoryIn);
        for (int i = 0; i < inventoryIns.size(); i++) {
            if (StringUtils.isNotNull(inventoryInMapper.selectInventoryInListListById(inventoryIns.get(i).getInventoryInId()))) {
                //存在，则存入其他入库对应产品信息
                inventoryIns.get(i).setInventoryInLists(inventoryInMapper.selectInventoryInListListById(inventoryIns.get(i).getInventoryInId()));
            }
        }
        return inventoryIns;
    }

    /**
     * 按编号查询
     *
     * @param inventoryInId 其他入库编号
     * @return
     */
    @Override
    public InventoryIn selectInventoryInById(String inventoryInId) {
        return inventoryInMapper.selectInventoryInById(inventoryInId);
    }

    /**
     * 其他入库新增
     *
     * @param inventoryIn
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInventoryIn(InventoryIn inventoryIn) throws Exception {
        String inventoryInId = idUtil.createInventoryInId();//自动生成编号
        if (StringUtils.isNotNull(inventoryInMapper.selectInventoryInById(inventoryInId))) {
            //存在
            return 0;
        }
        //不存在
        inventoryIn.setInventoryInId(inventoryInId);//主表添加编号
        inventoryInMapper.insertInventoryIn(inventoryIn);//其他入库
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(inventoryIn.getWarehouseId()); // 查询该仓库的产品列表
        List<InventoryInList> inventoryInLists = inventoryIn.getInventoryInLists();//其他入库产品信息
        for (int i = 0; i < inventoryInLists.size(); i++) {
            inventoryInLists.get(i).setInventoryInId(inventoryInId);//子表添加编号
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
            warehouseListDomain.setWarehouseId(inventoryIn.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryInLists.get(i).getProduceId());
            boolean isExistFlag = false;
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (inventoryInLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品入库的数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() + inventoryInLists.get(i).getInventoryInNumber());
                    isExistFlag = true;
                    break; //终止循环
                }
            }
            if (isExistFlag) {
                // 该产品存在，更新库存
                warehouseMapper.updateWarehouseList(warehouseListDomain);
            } else {
                // 该产品不存在，在库存中新增产品
                warehouseListDomain.setProductNumber(inventoryInLists.get(i).getInventoryInNumber());
                warehouseMapper.insertWarehouseList(warehouseListDomain);
            }
            inventoryInMapper.insertInventoryInList(inventoryInLists.get(i));//其他入库产品信息
        }
        return 1;

    }

    /**
     * 其他入库更新
     *
     * @param inventoryIn
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInventoryIn(InventoryIn inventoryIn) throws Exception {
        if (!StringUtils.isNotNull(inventoryInMapper.selectInventoryInById(inventoryIn.getInventoryInId()))) {
            //不存在
            return 0;
        }
        //存在其他入库单据
        inventoryInMapper.updateInventoryIn(inventoryIn);//其他入库单据
        List<InventoryInList> inventoryInListOld = inventoryInMapper.selectInventoryInListListById(inventoryIn.getInventoryInId());
        List<InventoryInList> inventoryInListNew = inventoryIn.getInventoryInLists();//其他入库产品信息
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(inventoryIn.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < inventoryInListNew.size(); i++) {
            InventoryInList inventoryInList = inventoryInMapper.selectInventoryInListById(inventoryIn.getInventoryInId(), inventoryInListNew.get(i).getProduceId());
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(inventoryIn.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryInListNew.get(i).getProduceId());
            if (StringUtils.isNotNull(inventoryInList)) {
                for (int j = 0; j < inventoryInListOld.size(); j++) {
                    if (inventoryInListOld.get(j).getProduceId().equals(inventoryInList.getProduceId())) {
                        inventoryInListOld.remove(j);
                        break;
                    }
                }
                //存在其他入库详细
                //更新
                for (WarehouseListDto s : warehouseListDtos) {
                    // 遍历仓库
                    if (inventoryInListNew.get(i).getProduceId().equals(s.getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品入库时增加的数量 + 新修改的入库数量
                        warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryInList.getInventoryInNumber() + inventoryInListNew.get(i).getInventoryInNumber());
                        break; //终止循环
                    }
                }
                warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
                inventoryInMapper.updateInventoryInList(inventoryInListNew.get(i));
            } else {//新增
                // 子表中不存在该产品，更新库存数，库存数 = 该产品新增时的入库数量
                boolean isExistFlag = false;
                for (WarehouseListDto s : warehouseListDtos) {
                    // 遍历仓库
                    if (inventoryInListNew.get(i).getProduceId().equals(s.getProduceId())) {
                        // 该产品存在，更新产品数量，产品数量 = 原库存数 + 新修改的入库数量
                        warehouseListDomain.setProductNumber(s.getProductNumber() + inventoryInListNew.get(i).getInventoryInNumber());
                        isExistFlag = true;
                        break; //终止循环
                    }
                }
                if (isExistFlag) {
                    // 该产品存在，更新库存数量
                    warehouseMapper.updateWarehouseList(warehouseListDomain);
                } else {
                    // 该产品不存在，在库存中新增产品
                    warehouseListDomain.setProductNumber(inventoryInListNew.get(i).getInventoryInNumber());
                    warehouseMapper.insertWarehouseList(warehouseListDomain);
                }
                inventoryInMapper.insertInventoryInList(inventoryInListNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < inventoryInListOld.size(); i++) {
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(inventoryIn.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryInListOld.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (inventoryInListOld.get(i).getProduceId().equals(s.getProduceId())) {
                    // 删除时，产品库存数 = 原有库存 - 该产品入库时的库存数
                    warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryInListOld.get(i).getInventoryInNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
            //最后将要删除的子标记录删除
            inventoryInMapper.deleteInventoryInListById(inventoryInListOld.get(i).getInventoryInId(), inventoryInListOld.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新其他入库删除标志位
     *
     * @param inventoryIn
     * @return
     */
    @Override
    public int updateInventoryInDeleteFlag(InventoryIn inventoryIn) {
        if (!StringUtils.isNotNull(inventoryInMapper.selectInventoryInById(inventoryIn.getInventoryInId()))) {
            //不存在
            return 0;
        }
        List<InventoryInList> inventoryInLists = inventoryIn.getInventoryInLists();// 被删除记录的所有子表记录
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(inventoryIn.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < inventoryInLists.size(); i++) {
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(inventoryIn.getWarehouseId());
            warehouseListDomain.setProduceId(inventoryInLists.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (inventoryInLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 删除时，产品库存数 = 原有库存 - 该产品入库时的库存数
                    warehouseListDomain.setProductNumber(s.getProductNumber() - inventoryInLists.get(i).getInventoryInNumber());
                    break;
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
        }
        return inventoryInMapper.updateInventoryInDeleteFlag(inventoryIn);
    }

    /**
     * 其他入库详细查询
     *
     * @param inventoryInList
     * @return
     */
    @Override
    public List<InventoryInList> selectInventoryInListList(InventoryInList inventoryInList) {
        return inventoryInMapper.selectInventoryInListList(inventoryInList);
    }

    /**
     * 查询其他入库对应的所有产品详细
     *
     * @param inventoryInId 其他入库编号
     * @return
     */
    @Override
    public List<InventoryInList> selectInventoryInListListById(String inventoryInId) {
        return inventoryInMapper.selectInventoryInListListById(inventoryInId);
    }

    /**
     * 查询其他入库对应的指定产品详细
     *
     * @param inventoryInId
     * @param produceId
     * @return
     */
    @Override
    public InventoryInList selectInventoryInListById(String inventoryInId, String produceId) {
        return inventoryInMapper.selectInventoryInListById(inventoryInId, produceId);
    }
}
